<template>
  <div class="login_container">
    <!-- 登录模块 -->
    <div class="login_box">
      <!-- 登录的logo -->
      <div class="login_logo">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录的表单 -->
      <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      // 登录的信息
      loginForm: {
        username: "admin",
        password: "123456",
      },
      // 登录的信息验证
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 登录
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return this.$message.error("请填写用户名和密码");

        const { data: res } = await this.$http.post("login", {
          username: this.loginForm.username,
          password: this.loginForm.password,
        });

        if (res.meta.status !== 200) return this.$message.error("登录失败");

        this.$message.success("登录成功!");
        // 保存token到会话中
        window.sessionStorage.setItem("token", res.data.token);
        // 跳转
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style scoped>
.login_container {
  height: 100%;
  background: #2a4b6a;
}
.login_box {
  width: 450px;
  height: 300px;
  background: #fff;
  border-radius: 6px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.login_logo {
  width: 150px;
  height: 150px;
  border: 1px solid #ccc;
  border-radius: 50%;
  padding: 10px;
  box-sizing: border-box;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
  box-shadow: 0 0 10px #eee;
}
.login_logo img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: #eee;
}
.el-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  box-sizing: border-box;
  padding: 20px;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>